查看原文
其他

自定义Istio Mixer Adapter示例教程(附源码)

陈洪波 几米宋 2022-09-07

作者:陈洪波

快速开始:https://micro-mesh/examples/adapter/auth 源码传送门。

研究Istio下构建简洁的微服务架构,对Istio的研究也更深入,自定义Mixer Adapter必不可少,以下结合使用场景做一个自定义适配器的实践分享。

背景

结合https://github.com/hb-go/micro-mesh的实践场景,需要在 ingressgatewayAPI service间加入认证&鉴权(JWT&RBAC),自然考虑Istio提供的安全方案,但使用JWT做认证鉴权在后端是无状态的,这样在使用场景上有一定限制,如:

  • 密码修改、终端连接限制等场景下无法踢除

  • 访问控制策略无法实时生效

默认方案只是在一些场景下不合适,根据具体需求考虑。

基于这样的场景可以自定义Adapter来实现,目标:

  • Token-JWT

  • 服务端验证token有效性

  • 应对密码修改、终端数量限制等场景

  • ACL-Casbin

  • 服务端获取用户角色,做API访问控制

  • 用户角色及接口授权策略实时生效

以下示例对token验证、访问控制不做具体设计,重点介绍如何自定义一个 auth-adapter

自定义Adapter介绍

配置关系及执行流程如图:

  • 属性:使用 istio的 attributes, istio/mixer/testdata/config/attributes.yaml

  • 属性与适配器输入映射模板:使用 istio的 authorization模板, istio/mixer/template/authorization/template.yaml,通过 template.proto查看协议内容

  • 适配器, micro-mesh/examples/adapter/auth/config/auth-adapter.yaml

  • go generate./...自动生成

  • 适配器服务启动配置, micro-mesh/examples/adapter/auth/config/config.proto

  • 适配器服务实例, micro-mesh/examples/adapter/auth/operatorconfig/cluster-service.yaml

  • 适配器配置, micro-mesh/examples/adapter/auth/operatorconfig/operator-cfg.yaml

目录结构
  1. bin 执行文件

  2. cmd

  3. └ main.go 适配器入口

  4. config 配置协议

  5. ├ adapter.auth.config.pb.html #go generate ./... 自动生成

  6. ├ auth-adapter.yaml 适配器描述文件 #go generate ./... 自动生成

  7. ├ config.pb.go #go generate ./... 自动生成

  8. ├ config.proto 适配器服务启动配置

  9. └ config.proto_descriptor #go generate ./... 自动生成

  10. operatorconfig k8s配置

  11. ├ attributes.yaml 属性 #copy istio/mixer/testdata/config/attributes.yaml

  12. ├ cluster-service.yaml 适配器服务实例

  13. ├ operator-cfg.yaml 适配器配置

  14. └ template.yaml 属性与适配器输入模板 #copy istio/mixer/template/authorization/template.yaml

  15. testdata 测试配置

  16. ├ attributes.yaml 属性 #copy istio/mixer/testdata/config/attributes.yaml

  17. ├ auth-adapter.yaml 适配器描述文件 #copy config/auth-adapter.yaml

  18. ├ operator-cfg.yaml 适配器配置

  19. └ template.yaml 属性与适配器输入模板 #copy istio/mixer/template/authorization/template.yaml

  20. auth.go 适配器服务实现

  21. Dockerfile Docker镜像

有3处与适配器实现相关:

  • 适配器服务启动配置 config/config.proto

  • 适配器服务实现 auth.go

  • 适配器入口 cmd/main.go


接下来使用micro-mesh/examples/adapter/auth源码按步骤操作,实现本地及 K8S环境的测试部署。

步骤

开发环境

  • OSX

  • Go 1.11.1

  • protoc libprotoc 3.6.1

  • Istio 1.0.6

1.Istio源码

  1. mkdir -p $GOPATH/src/istio.io/

  2. cd $GOPATH/src/istio.io/

  3. git clone https://github.com/istio/istio.git

2.micro-mesh源码

  1. git clone https://github.com/hb-go/micro-mesh.git

3.Mixer开发工具

  1. # build mixer server & client

  2. cd istio

  3. make mixs

  4. make mixc

$GOPATH/out/darwin_amd64/release/生成 mixsmixc

4.构建Auth adapter项目

  1. # copy auth adapter example

  2. cp {micro-mesh path}/examples/adapter/auth mixer/adapter/auth


  3. cd mixer/adapter/auth

Optional

可以删除 config目录除 config.proto外的其他文件,看执行go generate后的结果

  1. go generate ./...

  2. go build ./...

go generate根据 config/config.proto以及 auth.go的注释自动生成 config目录下的其他文件:

  • adapter.auth.config.pb.html

  • auth-adapter.yaml

  • config.pb.go

  • config.proto_descriptor

根据 auth.go的以下注释, mixer_codegen.sh使用 authorization模板生成 nameauth-adapter的适配器。

  1. // nolint:lll

  2. // Generates the auth adapter's resource yaml. It contains the adapter's configuration, name, supported template

  3. // names (metric in this case), and whether it is session or no-session based.

  4. //go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -a mixer/adapter/auth/config/config.proto -x "-s=false -n auth-adapter -t authorization"

5.本地测试

本地测试使用testdata下的配置,其中 operator-cfg.yaml有几处与正式部署不同:

  • handler的 address使用本地服务 "[::]:44225"

  • 为了方便测试 instance的 params参数以及 rule的 math条件做了简化

  1. # 启动适配器服务

  2. go run cmd/main.go 44225


  3. # 使用testdata下配置启动mixer server

  4. $GOPATH/out/darwin_amd64/release/mixs server \

  5. --configStoreURL=fs://$GOPATH/src/istio.io/istio/mixer/adapter/auth/testdata \

  6. --log_output_level=attributes:debug


  7. # 测试Adapter是否生效

  8. $GOPATH/out/darwin_amd64/release/mixc check -s request.host="localhost" --stringmap_attributes "request.headers=x-custom-token:efg"

  9. # Check RPC completed successfully. Check status was PERMISSION_DENIED (mm-example-auth.handler.istio-system:Unauthorized...)


  10. $GOPATH/out/darwin_amd64/release/mixc check -s request.host="localhost" --stringmap_attributes "request.headers=x-custom-token:abc"

  11. # Check RPC completed successfully. Check status was OK

NOTE:出现预期结果不一致可能是由于mixer cache导致 Validusecount:10000,valid duration:9.726875254s,请参考Istio Mixer Cache系列文章了解。

6.打包镜像

  1. # build执行文件

  2. CGO_ENABLED=0 GOOS=linux \

  3. go build -a -installsuffix cgo -v -o bin/auth ./cmd/


  4. # docker镜像

  5. docker build -t hbchen/micro-mesh-example-adapter-auth:v0.0.1 .

  6. docker push hbchen/micro-mesh-example-adapter-auth:v0.0.1

7.Istio环境部署

部署环境

  • GKE 1.11.7-gke.4

  • Istio 1.0.0

  1. # 属性、模板

  2. # attributes.yaml -> istio/mixer/testdata/config/attributes.yaml

  3. # template.yaml -> istio/mixer/template/authorization/template.yaml

  4. kubectl apply -f examples/adapter/auth/testdata/attributes.yaml -f examples/adapter/auth/testdata/template.yaml


  5. # 适配器

  6. kubectl apply -f examples/adapter/auth/config/auth-adapter.yaml

这里是以micro-mesh示例为基础的配置,如果使用 bookinfo或者自己的服务需要做相应的修改

operator-cfg.yaml与本地测试配置不同:

  • handler的 address使用集群服务 "mm-example-auth-adapter-service:44225"

  • instance的 params根据 authorization模板及 auth-adapter服务的需求配置

  • rule的 match条件使用 destination.service=="mm-example-api.default.svc.cluster.local",仅对 mm-example-api服务生效

  1. # 适配器服务实例部署

  2. kubectl apply -f examples/adapter/auth/operatorconfig/cluser-service.yaml


  3. # 适配器配置

  4. kubectl apply -f examples/adapter/auth/operatorconfig/operator-cfg.yaml

8.Istio环境部署测试

如果没有开Gateway的JWT验证可以忽略 Authorization,其实做了自定义Auth后是多余的😂

  1. TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.1/security/tools/jwt/samples/demo.jwt -s)


  2. curl -H "Authorization: Bearer $TOKEN" -H "x-custom-token: efg" -X GET http://35.193.180.18/v1/example/call/Hobo

  3. curl -H "Authorization: Bearer $TOKEN" -H "x-custom-token: abc" -X GET http://35.193.180.18/v1/example/call/Hobo

参考

  • Mixer Out of Process Adapter Walkthrough

  • Simple Istio Mixer Out of Process Authorization Adapter

点击阅读原文跳转到 Web 浏览可以查看文中链接。

相关阅读推荐

如何创建Istio Mixer适配器——来自Circonus公司的分享

Istio Mixer Cache工作原理与源码分析Part1-基本概念

Istio Mixer Cache工作原理与源码分析part2-工作原理

Istio源码解析系列part3—Mixer工作流程浅析

加入 ServiceMesher 社区

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存